//
//  ViewController.m
//  LOL_PalmarHelper
//
//  Created by dlios on 15-6-20.
//  Copyright (c) 2015年 hantao. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()


@end

@implementation ViewController

-(void)dealloc
{
    [_webView release];
    [_newsId release];
    [_titleLabel release];
    [_label1 release];
    [_label2 release];
    [_label3 release];
    [_timeLabel release];
    [_fromLabel release];
    [_scrollV release];
    [_myView release];
    [_str release];
    [_activityIndicator release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"内容";
    self.view.frame =[[UIScreen mainScreen]bounds];
    
    self.scrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
    if (self.view.frame.size.width == 736) {
        self.scrollV.contentSize = CGSizeMake(0,1000);

    }else{
        
        self.scrollV.contentSize = CGSizeMake(0,667+90);
        
    }
    
   
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(buttonAct)];
    

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fenxiang.png"] style:UIBarButtonItemStyleDone target:self action:@selector(rightButton)];
    
    
    
    
    self.scrollV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scrollV];
    self.scrollV.delegate = self;
    self.scrollV.bounces = NO;
    self.scrollV.showsVerticalScrollIndicator = FALSE;
    
    self.webView = [[UIWebView alloc]init];
    
    if (self.view.frame.size.height == 480) {
        
        
        self.webView.frame = CGRectMake(0, 90, self
                                   .view.frame.size.width, self.view.frame.size.height+50);
        
    }else if (self.view.frame.size.height == 568){
        
        self.webView.frame = CGRectMake(0, 90, self.view.frame.size.width, self.view.frame.size.height); 
        
        
    }else if (self.view.frame.size.height == 667){
        
        self.webView.frame = CGRectMake(0, 90, self.view.frame.size.width, self.view.frame.size.height-113);
    }else if (self.view.frame.size.height == 736){
        
        self.webView.frame = CGRectMake(0, 90, self.view.frame.size.width, self.view.frame.size.height);
      
    }
    
                    
                    
    self.webView.backgroundColor = [UIColor whiteColor];
    
    [self.scrollV  addSubview:self.webView];
    self.webView.delegate =self;
    self.webView.scrollView.bounces = NO;
    self.webView.scrollView.userInteractionEnabled = NO;
    self.webView.userInteractionEnabled = YES;
    self.webView.scrollView.clipsToBounds = NO;



    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5,self.scrollV.frame.size.width-10 , 60)];
    self.titleLabel.backgroundColor = [UIColor whiteColor];
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.font = [UIFont systemFontOfSize:20.0];
    [self.scrollV addSubview:self.titleLabel];
    [_titleLabel release];
    
    
    self.label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 60,70 , 30)];
    self.label1.backgroundColor = [UIColor whiteColor];
   
    self.label1.font = [UIFont systemFontOfSize:15];
    [self.scrollV addSubview:self.label1];
    [_label1 release];
    
    self.label2 = [[UILabel alloc] initWithFrame:CGRectMake(170, 60,50, 28)];
    self.label2.backgroundColor = [UIColor whiteColor];
    
    self.label2.font = [UIFont systemFontOfSize:15];
    [self.scrollV  addSubview:self.label2];
    [_label2 release];
    
    
    self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 60,90 , 30)];
    self.timeLabel.backgroundColor = [UIColor whiteColor];
    
    self.timeLabel.font = [UIFont systemFontOfSize:13];
    [self.scrollV addSubview:self.timeLabel];
    [_timeLabel release];
    

    self.fromLabel = [[UILabel alloc] initWithFrame:CGRectMake(210, 60, 70, 30)];
    self.fromLabel.backgroundColor = [UIColor clearColor];
    self.fromLabel.font = [UIFont systemFontOfSize:13];
    [self.scrollV addSubview:self.fromLabel];
    [_fromLabel release];
    
    
    
    self.label3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 89, self.view.frame.size.width, 2)];
    self.label3.backgroundColor = [UIColor lightGrayColor];
   
    

    
    
      
    [self  getInformation];
    
    
}



-(void)rightButton
{

    
    
    NSString *url = [NSString stringWithFormat:@"%@:来源于盒尔盟",self.titleLabel.text];

    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"559a76b067e58e5d91002844"
                                      shareText:url
                                     shareImage:[UIImage imageNamed:@"IPhoneApp@2x.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,nil]
                                       delegate:self];





}


-(void)getInformation
{
    
    NSString *url_string = [NSString stringWithFormat:@"http://lolbox.oss.aliyuncs.com/json/v3/news/content/%@.json",self.newsId];

    self.shareUrl = url_string;
    [AFN AFNGetData:url_string andBlock:^(id respondes) {
        
        if (![respondes isKindOfClass:[@"error" class]]) {
            
            
            NSMutableDictionary  *dic = respondes;
           self.titleLabel.text = [dic objectForKey:@"title"];
            self.timeLabel.text = [dic objectForKey:@"posttime"];
            self.fromLabel.text = [dic objectForKey:@"site"];
            self.str =[dic objectForKey:@"content"];
            
             self.label1.text = @"更新时间: ";
            self.label2.text = @"来源: ";
            [self.scrollV  addSubview:self.label3];
            [self.webView loadHTMLString:self.str baseURL:nil];
          
           
       
                      
        }else if ([respondes isEqualToString:@"error"]) {
            
           
            UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [self.view addSubview:al];
            [al show];
            [al release];
            
            UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, 450)];
            imgV.image = [UIImage imageNamed:@"f10648560d33605f97d55c5338552d89.jpg"];
            [self.view addSubview:imgV];
            
            
            
            
            
        }

        
    }];
    
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    
    if (self.scrollV.contentOffset.y < 80 ) {
        self.webView.scrollView.userInteractionEnabled = NO;
    }else
    {
        self.webView.scrollView.userInteractionEnabled = YES;
    }
    
    
}

-(void)webViewDidStartLoad:(UIWebView *)webView
{
    

    self.viewV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.scrollV.frame.size.height)];
    [self.viewV setTag:108];
    [self.viewV setBackgroundColor:[UIColor blackColor]];
    [self.viewV setAlpha:0.5];
    [self.scrollV addSubview:self.viewV];
    
    
    
    
    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [self.activityIndicator setCenter:self.view.center];
    [self.activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [self.scrollV addSubview:self.activityIndicator];
    
    [self.activityIndicator startAnimating];
    
    
}
-(void)buttonAct
{
    self.webView.delegate =nil;
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    
    
    
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
   
    [webView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth;"
     "var maxwidth= 300;"//缩放系数,调动这个参数可以改变图片的宽度
     "for(i=0;i <document.images.length;i++){"
     "myimg = document.images[i];"
     "oldwidth = myimg.width;"
     "if (oldwidth >= 300) {"
     "myimg.width = maxwidth;"
     "myimg.height = myimg.height;"
     "}"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    
    [self.activityIndicator stopAnimating];
    
    NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'";
    [webView stringByEvaluatingJavaScriptFromString:str];//改变webView字体大小
//    480   568
   
        
        
        
    [self.viewV removeFromSuperview];
    

    
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    
    
    [self.activityIndicator stopAnimating];
    UIView *view = (UIView*)[self.myView viewWithTag:108];
    [view removeFromSuperview];

    
    
}

-(CGFloat)heightFor:(NSString *)content
{
    //自适应
    CGSize size = CGSizeMake(300, 100000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17.0] forKey:NSFontAttributeName];
    CGRect frame = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return frame.size.height+49+64;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
